package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.service.FileUploadService;
import com.atguigu.spzx.manager.util.MinioParamProperties;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.http.Method;
import lombok.SneakyThrows;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileUploadServiceImpl implements FileUploadService {

/*    @Autowired
    private MinioParamProperties minioParamProperties;*/

    @Override
    @SneakyThrows
    public String fileUpload(MultipartFile multipartFile) {


        // 客户端
        MinioClient minioClient =  MinioClient.builder()
                .endpoint("http://192.168.83.123:9001")
                .credentials("admin","admin123456")
                .build();

        // 参数
        String originalFilename = multipartFile.getOriginalFilename();
        String timeStr = new DateTime().toString("/yyyy/MM/dd/");
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket("spzx-1")
                .stream(multipartFile.getInputStream(), multipartFile.getSize(), -1)
                .object("avatar"+timeStr+originalFilename)
                .build();

        // 上传
        minioClient.putObject(putObjectArgs);

        // url

        String url = "http://192.168.83.123:9001/"+"spzx-1"+"/"+"avatar"+timeStr+originalFilename;

        return url;

    }



/*    @SneakyThrows
    @Override
    public String fileUpload(MultipartFile multipartFile) {
        String urlPrefix = minioParamProperties.getUrlPrefix();
        String accessKey = minioParamProperties.getAccessKey();
        String secretKey = minioParamProperties.getSecretKey();
        //创建minio客户端
        MinioClient minioClient = MinioClient.builder()
                .endpoint(urlPrefix)
                .credentials(accessKey, secretKey)
                .build();
        //设置上传参数
        String bucketName= minioParamProperties.getBucketName();
        String imagePath= minioParamProperties.getImagePath();
        String datetime = new DateTime().toString("/yyyy/MM/dd/");
        String originalFilename = multipartFile.getOriginalFilename();
        PutObjectArgs putObjectArgs=PutObjectArgs.builder()
                .bucket(bucketName)
                //注意.contentType()  这个一定要写,不然默认是下载的文件头了
                .contentType(multipartFile.getContentType())
                .stream(multipartFile.getInputStream(),multipartFile.getSize(), -1)
                .object(imagePath+datetime+originalFilename)
                .build();
        //上传图片
        minioClient.putObject(putObjectArgs);
        //封装返回url
        String url=urlPrefix+"/"+bucketName+"/"+imagePath+datetime+originalFilename;
        return url;
    }*/
}
